using Microsoft.Xna.Framework;
using RebelStar.Engine.Graphics;

namespace RebelStar.Engine.Maps
{
    class MapPreview
    {
        readonly Map map;
        Rectangle bounds;

        public MapPreview(Map map, Rectangle bounds)
        {
            this.map = map;
            this.bounds = bounds;
        }
        public MapPreview(Map map, int x, int y, int width, int height)
            : this(map, new Rectangle(x, y, width, height))
        {
        }

        public void Draw(ISurface surface)
        {
            float scale = bounds.Width / (float)(map.Width * MapEntry.WIDTH);
            map.Draw(surface, 0, 0, map.Width, map.Height, scale); // bounds.X, bounds.Y);

            #region

            //                int points = 8;
            //                short[] lineListIndices;
            //                VertexPositionNormalTexture[] pointList;
            //                VertexDeclaration vertexDeclaration;
            //                VertexBuffer vertexBuffer;
            //                BasicEffect basicEffect;
            //
            //                basicEffect = new BasicEffect(device, null);
            //                basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
            //
            ////                basicEffect.World = worldMatrix;
            ////                basicEffect.View = viewMatrix;
            ////                basicEffect.Projection = projectionMatrix;
            //
            //
            //                // Initialize an array of indices of type short.
            //                lineListIndices = new short[(points*2) - 2];
            //
            //                // Populate the array with references to indices in the vertex buffer
            //                for (int i = 0; i < points - 1; i++)
            //                {
            //                    lineListIndices[i*2] = (short) (i);
            //                    lineListIndices[(i*2) + 1] = (short) (i + 1);
            //                }
            //
            //                vertexDeclaration = new VertexDeclaration(
            //                    device,
            //                    VertexPositionNormalTexture.VertexElements
            //                    );
            //
            //                pointList = new VertexPositionNormalTexture[points];
            //
            //                for (int x = 0; x < points/2; x++)
            //                {
            //                    for (int y = 0; y < 2; y++)
            //                    {
            //                        pointList[(x*2) + y] = new VertexPositionNormalTexture(
            //                            new Vector3(x, y, 0),
            //                            Vector3.Forward,
            //                            Vector2.One
            //                            );
            //                    }
            //                }
            //
            //                // Initialize the vertex buffer, allocating memory for each vertex.
            //                vertexBuffer = new VertexBuffer(device,
            //                                                VertexPositionNormalTexture.SizeInBytes*(pointList.Length),
            //                                                BufferUsage.None);
            //
            //                // Set the vertex buffer data to the array of vertices.
            //                vertexBuffer.SetData<VertexPositionNormalTexture>(pointList);
            //
            //                device.VertexDeclaration = vertexDeclaration;
            //
            //                basicEffect.Begin();
            //                foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            //                {
            //                    pass.Begin();
            //
            //                    device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
            //                        PrimitiveType.LineList,
            //                        pointList,
            //                        0, // vertex buffer offset to add to each element of the index buffer
            //                        8, // number of vertices in pointList
            //                        lineListIndices, // the index buffer
            //                        0, // first index element to read
            //                        7  // number of primitives to DrawSprite
            //                        );
            //
            //                    pass.End();
            //                }
            //                basicEffect.End();

            #endregion

            map.Changed = false;
        }

        public int X
        {
            get { return bounds.Left; }
        }
        public int Y
        {
            get { return bounds.Top; }
        }
        public int Width
        {
            get { return bounds.Right-bounds.Left; }
        }
        public int Height
        {
            get { return bounds.Bottom-bounds.Top; }
        }
        public bool Changed
        {
            get { return map.Changed; }
        }
    }
}
